import re

class NameParser():
  def __init__(self,title):
    self.title = str(title)
    self.type = ''
    self.version = ''
    self.season = -1
    self.episode = -1
    self.format = ''
    self.resolution = ''
    self.groups = []
    self.is_repack = False
    self.known_groups = [
      'lol',
      'alliance',
      'eztv',]
    self.lowercase()
    self.replace_brackets()
    self.filter_underscores()
    self.filter_dots()
    self.filter_dashes()

    self.make_components()
    self.format_title()
 
  def lowercase(self):
    self.title = self.title.lower()
   
  def filter_underscores(self):
    self.title = self.title.replace('_',' ')
 
  def filter_dots(self):
    self.title = self.title.replace('.',' ')
   
  def filter_dashes(self):
    self.title = self.title.replace('-',' ')
   
  def make_components(self):
    self.components = self.title.split(' ')
  
  def match(self,other):
    if self.title == other.title:
      if self.season == other.season:
        if self.episode == other.episode:
          return True
        else:
          return False
      else:
        return False
    else:
      return False
        
   
  def format_title(self):
    self.new = ''
    get_title = True
    for component in self.components:
      if re.match('^[0-9][0-9][0-9]$',component) != None:
        get_title = False
        self.season = int(component[0])
        self.episode = int(component[1:])
      elif re.match('^s[0-9][0-9](e|x)[0-9][0-9]$',component) != None:
        get_title = False
        self.season = int(component[1:3])
        self.episode = int(component[4:])
      elif re.match('^[0-9]{3,4}(i|p)$',component) != None:
        get_title = False
        self.resolution = component[0:-1]
      elif component == 'repack':
        get_title = False
        self.is_repack = True
      elif component == 'hdtv':
        get_title = False
        # dont care, everyone adds this
        continue
      elif re.match('^[0-9]{3,4}x[0-9]{3,4}$',component) != None:
        get_title = False
        self.resolution = component[component.find('x')+1:]
      elif (component == 'xvid') | (component == 'divx') | (component == 'avi'):
        get_title = False
        self.format = 'avi'
      elif (component == 'x264') | (component == 'h264') | (component == 'mkv'):
        get_title = False
        self.format = 'mkv'
      else:
        if not get_title:
          self.groups.append(component)
        else:
          self.new += component + ' '
    self.title = self.new.strip().replace('  ',' ')
 
  def replace_brackets(self):
    self.title = self.title.replace('[',' ')
    self.title = self.title.replace(']',' ') 
   
  def __str__(self):
    output = self.title
    if self.season > -1:
      output += '.'+str(self.season)
    if self.episode > -1:
      output += 'x'+str(self.episode)
    if len(self.resolution) > 0:
      output += '.'+self.resolution
    if len(self.format) > 0:
      output += '.'+self.format
    if self.is_repack:
      output += '.repack'
     
    return output
